﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Compression;

namespace LuKou.Web.Filter
{
    public class CompressWhitespaceFilter : Stream
    {
        private Stream _contentStream;

        public CompressWhitespaceFilter(Stream contentStream, CompressOptions compressOptions)
        {
            if (compressOptions == CompressOptions.GZip)
            {
                this._contentStream = new GZipStream(contentStream, CompressionMode.Compress);
            }
            else if (compressOptions == CompressOptions.Deflate)
            {
                this._contentStream = new DeflateStream(contentStream, CompressionMode.Compress);
            }
            else
            {
                this._contentStream = contentStream;
            }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] data = new byte[count + 1];
            Buffer.BlockCopy(buffer, offset, data, 0, count);

            string strtext = System.Text.Encoding.UTF8.GetString(data);

            #if !DEBUG
            strtext = Regex.Replace(strtext, "^\\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            //strtext = Regex.Replace(strtext, "[\\r|\\n]", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            strtext = Regex.Replace(strtext, "<!--*.*?-->", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            #endif

            byte[] outdata = System.Text.Encoding.UTF8.GetBytes(strtext.Trim());
            this._contentStream.Write(outdata, 0, outdata.Length);
        }

        public override bool CanRead
        {
            get {return this._contentStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return this._contentStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return this._contentStream.CanWrite; }
        }

        public override void Flush()
        {
            this._contentStream.Flush();
        }

        public override long Length
        {
            get { return this._contentStream.Length; }
        }

        public override long Position
        {
            get
            {
                return this._contentStream.Position;
            }
            set
            {
                this._contentStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return this._contentStream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return this._contentStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            this._contentStream.SetLength(value);
        }
    }

    public enum CompressOptions
    {
        GZip,
        Deflate,
        None
    }
}
